//
//  NSString+WeiboCountWord.m
//  FriendReadDemo
//
//  Created by 李家明 on 12-3-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NSString+WeiboCountWord.h"

@implementation NSString (WeiboCountWork)

- (int)wordsCount
{
	// Source: http://www.cocoachina.com/macdev/cocoa/2011/0110/2552.html
	int i, n = [self length], l = 0, a = 0, b = 0;
    
	unichar c;
    
	for (i = 0; i < n; i++)
	{
		c = [self characterAtIndex:i];
        
		if (isblank(c))
		{
			b++;
		}
		else if (isascii(c))
		{
			a++;
		}
		else
		{
			l++;
		}
	}
    
	if (a == 0 && l == 0)
	{
		return 0;
	}
    
	return l + (int)ceilf((float)(a + b) / 2.0);
}

@end